package app.fugema.shop.main.online.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.base.common.model.http.jackSon.JacksonUtils;
import com.base.common.utils.LogUtil;
import com.base.common.utils.UIUtils;
import com.base.common.view.adapter.ada.BaseRVAdapter;
import com.base.common.view.adapter.ada.SingleUniversalAdapter;
import com.base.common.view.base.BaseTitleActivity;
import com.jeremyliao.liveeventbus.LiveEventBus;
import com.tencent.lbssearch.TencentSearch;
import com.tencent.lbssearch.httpresponse.BaseObject;
import com.tencent.lbssearch.httpresponse.HttpResponseListener;
import com.tencent.lbssearch.object.param.SearchParam;
import com.tencent.lbssearch.object.result.SearchResultObject;

import java.util.ArrayList;
import java.util.List;

import app.fugema.shop.R;
import app.fugema.shop.constants.AppConstants;
import app.fugema.shop.databinding.ActivityAddrSearchDetailBinding;
import app.fugema.shop.main.home.model.PoiPlaceBean;
import app.fugema.shop.main.online.vm.OnlineVM;
import app.fugema.shop.util.SimpleTextWatcher;

public class SearchAddressActivity extends BaseTitleActivity<ActivityAddrSearchDetailBinding, OnlineVM> {

    public static void start(Context context ,String city)
    {
        Intent intent = new Intent(context, SearchAddressActivity.class);
        intent.putExtra("regionCity",city);
        context.startActivity(intent);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_addr_search_detail);
    }

    private SingleUniversalAdapter<PoiPlaceBean> adapter;

    private TencentSearch tencentSearch;

    @Override
    protected int getStatusBarColor()
    {
        return R.color.redColor;
    }

    @Override
    protected void initView()
    {
        super.initView();
        setCloseImg(R.mipmap.ic_back_white);
        setTitle("搜索地址",R.color.white);
        adapter = new SingleUniversalAdapter<PoiPlaceBean>()
        {
            @Override
            public int getLayoutId()
            {
                return R.layout.item_location_city_view;
            }

            @Override
            public void onItemClick(View view, BaseRVAdapter adapter, int onclickType, int position, PoiPlaceBean bean)
            {
                super.onItemClick(view, adapter, onclickType, position, bean);

                UIUtils.showToastSafes( bean.getTitle() );

                LiveEventBus.get(AppConstants.EventKey.POI_SELECT,PoiPlaceBean.class).post( bean );
                finish();
            }
        };
        binding.rvRecyclerView.setAdapter(adapter);

        binding.etSearch.addTextChangedListener(new SimpleTextWatcher()
        {
            @Override
            public void afterTextChanged(String content)
            {
                 txSearchPoi(content);
            }
        });

        //地址关键词搜索
        tencentSearch = new TencentSearch(this);
    }

    private String  rergionCity;

    @Override
    protected void initData(Intent intent)
    {
        super.initData(intent);
        rergionCity = intent.getStringExtra("regionCity");


    }

    private void txSearchPoi(String keyWord)
    {

        //城市搜索
        SearchParam.Region region = new SearchParam
                //设置搜索城市
                .Region(rergionCity)
                //设置搜索范围不扩大
                .autoExtend(false);

        //构建地点检索
        SearchParam searchParam = new SearchParam(keyWord, region);
        tencentSearch.search(searchParam, new HttpResponseListener<BaseObject>() {

            @Override
            public void onFailure(int arg0, String arg2,Throwable arg3)
            {
                UIUtils.showToastSafes( arg2 );
            }

            @Override
            public void onSuccess(int arg0, BaseObject arg1)
            {
                if (arg1 == null)
                {
                    return;
                }
                SearchResultObject obj = (SearchResultObject) arg1;
                if(obj.data == null)
                {
                    return;
                }
                List<PoiPlaceBean> poiList = new ArrayList<>();
                //将其他检索到的地点在地图上用 marker 标出来
                for(SearchResultObject.SearchResultData data : obj.data)
                {
                    //LogUtil.e("SearchDemo","data:"+ JacksonUtils.transBean2Json(data));
                    PoiPlaceBean poi =new PoiPlaceBean();
                    poi.setTitle( data.title );
                    poi.setAddress( data.address );
                    poi.setLat( data.latLng.getLatitude() );
                    poi.setLng(data.latLng.getLongitude());
                    poiList.add( poi );
                }
                adapter.clear();
                adapter.setDataList( poiList );
            }
        });

    }


}
